package com.cqy.spring.cloud.web;

import com.cqy.spring.cloud.feign.TestFeign;
import com.cqy.spring.cloud.vo.Cqy;
import com.cqy.spring.cloud.vo.Demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/client")
public class ClientController implements TestFeign {

    public Map<String, Object> test(Cqy cqy) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(cqy.getName(), cqy.getAge());
        return map;
    }

    @Override
    public Demo create(Demo demo) {
        log.info("Create executed : " + demo);
        return demo;
    }

    @Override
    public Demo update(Demo demo) {
        log.info("Update execute :" + demo);
        return demo;
    }

    @Override
    public Demo delete(String id) {
        log.info("Delete execute : " + id);
        return Demo.builder().name("demo-" + id).data("data-" + id).build();
    }

    @Override
    public Demo get(String id, String header) {
        Demo demo = Demo.builder()
                .name(header)
                .data(header).build();
        System.out.println("Get execute : " + id + "," + header);
        return demo;
    }

    @Override
    public List<Demo> list() {
        System.out.println("List execute");
        List<Demo> demos = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Demo demo = Demo.builder()
                    .name("demo-" + i)
                    .data("data" + i).build();
            demos.add(demo);
        }
        return demos;
    }

    @Override
    public String upload(MultipartFile file) {
        return file.getOriginalFilename();
    }
}
